<?php

namespace App\Http\Resources\Apps;

use App\Models\Order;
use Illuminate\Http\Resources\Json\JsonResource;

class ProductResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $order = collect($this['order'])->firstWhere('sale_user_id',$this['user_id']);
        return [
            'id'        => $this['id'],
            'name'      => $this['name'],
            'preview'   => $this['preview'],
            'price'     => $this['price'],
            'images'    => $this['images'],
            'integral'  => $this['integral'],
            'increase'  => $this['increase'],
            'pick'      => $this['pick'],
            'order'     => $order ? OrderResource::make(Order::find($order->order_no)) : (object) [],
            'transfer'  => $this['transfer'],
        ];
    }
}
